#!/bin/bash

# NiuLink UEFI Fix Verification Script
# 验证UEFI修复是否正确应用
# 版本: v1.2
# 日期: 2025-06-06

set -e

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# 日志函数
log_info() {
    echo -e "${BLUE}[INFO]${NC} $1"
}

log_success() {
    echo -e "${GREEN}[✅]${NC} $1"
}

log_warning() {
    echo -e "${YELLOW}[⚠️]${NC} $1"
}

log_error() {
    echo -e "${RED}[❌]${NC} $1"
}

# 检查参数
if [ $# -ne 1 ]; then
    echo "使用方法: $0 <fixed_iso_file>"
    echo "示例: $0 NiuLinkOS-uefi-fixed.iso"
    exit 1
fi

FIXED_ISO="$1"

# 检查输入文件
if [ ! -f "$FIXED_ISO" ]; then
    log_error "ISO文件不存在: $FIXED_ISO"
    exit 1
fi

echo "========================================"
echo "     NiuLink UEFI修复验证报告"
echo "========================================"
echo "验证文件: $FIXED_ISO"
echo "文件大小: $(du -h "$FIXED_ISO" | cut -f1)"
echo "验证时间: $(date)"
echo

# 创建临时目录
TEMP_DIR=$(mktemp -d)
trap "sudo rm -rf $TEMP_DIR" EXIT

log_info "开始验证修复内容..."

# 挂载ISO
log_info "挂载ISO文件..."
ISO_MOUNT="$TEMP_DIR/iso_mount"
mkdir -p "$ISO_MOUNT"
sudo mount -o loop "$FIXED_ISO" "$ISO_MOUNT" 2>/dev/null
if [ $? -eq 0 ]; then
    log_success "ISO挂载成功"
else
    log_error "ISO挂载失败"
    exit 1
fi

# 提取squashfs
log_info "提取squashfs文件系统..."
SQUASHFS_DIR="$TEMP_DIR/squashfs"
sudo unsquashfs -d "$SQUASHFS_DIR" "$ISO_MOUNT/LiveOS/squashfs.img" >/dev/null 2>&1
if [ $? -eq 0 ]; then
    log_success "squashfs提取成功"
else
    log_error "squashfs提取失败"
    exit 1
fi

# 挂载rootfs
log_info "挂载rootfs..."
ROOTFS_MOUNT="$TEMP_DIR/rootfs_mount"
mkdir -p "$ROOTFS_MOUNT"
sudo mount -o loop "$SQUASHFS_DIR/LiveOS/rootfs.img" "$ROOTFS_MOUNT" 2>/dev/null
if [ $? -eq 0 ]; then
    log_success "rootfs挂载成功"
else
    log_error "rootfs挂载失败"
    exit 1
fi

# 验证修复内容
echo
echo "🔍 核心修复验证:"
echo "----------------------------------------"

# 1. 验证fedora.py EFI目录修复
echo -n "1. fedora.py EFI目录修复: "
FEDORA_PY="$ROOTFS_MOUNT/usr/lib64/python2.7/site-packages/pyanaconda/installclasses/fedora.py"
if [ -f "$FEDORA_PY" ]; then
    if sudo grep -q 'efi_dir = "centos"' "$FEDORA_PY"; then
        log_success "EFI目录已修复为centos"
    else
        log_error "EFI目录修复失败"
    fi
else
    log_error "fedora.py文件不存在"
fi

# 2. 验证fedora安装类隐藏
echo -n "2. fedora安装类隐藏: "
if sudo grep -q 'hidden = True' "$FEDORA_PY"; then
    log_success "fedora安装类已强制隐藏"
else
    log_warning "fedora安装类隐藏状态不确定"
fi

# 3. 验证CentOS安装类可见性
echo -n "3. CentOS安装类可见性: "
CENTOS_PY="$ROOTFS_MOUNT/usr/lib64/python2.7/site-packages/pyanaconda/installclasses/centos.py"
if [ -f "$CENTOS_PY" ]; then
    if sudo grep -q 'if False:.*Always show CentOS' "$CENTOS_PY"; then
        log_success "CentOS安装类强制可见"
    else
        log_warning "CentOS安装类使用默认可见性"
    fi
else
    log_error "centos.py文件不存在"
fi

# 4. 验证系统标识
echo -n "4. 系统标识: "
if sudo grep -q 'ID="centos"' "$ROOTFS_MOUNT/etc/os-release"; then
    log_success "系统标识为CentOS"
else
    log_warning "系统标识可能不正确"
fi

# 5. 验证双启动支持
echo
echo "🚀 启动支持验证:"
echo "----------------------------------------"

echo -n "5. UEFI启动支持: "
if [ -f "$ISO_MOUNT/images/efiboot.img" ]; then
    log_success "UEFI启动镜像存在"
else
    log_error "UEFI启动镜像缺失"
fi

echo -n "6. Legacy BIOS支持: "
if [ -f "$ISO_MOUNT/isolinux/isolinux.bin" ]; then
    log_success "Legacy BIOS启动支持"
else
    log_error "Legacy BIOS启动支持缺失"
fi

# 6. 验证关键文件完整性
echo
echo "📁 文件完整性验证:"
echo "----------------------------------------"

echo -n "7. anaconda安装程序: "
if [ -d "$ROOTFS_MOUNT/usr/lib64/python2.7/site-packages/pyanaconda" ]; then
    log_success "anaconda安装程序完整"
else
    log_error "anaconda安装程序缺失"
fi

echo -n "8. 内核文件: "
if [ -f "$ISO_MOUNT/isolinux/vmlinuz" ]; then
    log_success "内核文件存在"
else
    log_error "内核文件缺失"
fi

echo -n "9. initrd文件: "
if [ -f "$ISO_MOUNT/isolinux/initrd.img" ]; then
    log_success "initrd文件存在"
else
    log_error "initrd文件缺失"
fi

# 清理挂载点
sudo umount "$ROOTFS_MOUNT" 2>/dev/null || true
sudo umount "$ISO_MOUNT" 2>/dev/null || true

echo
echo "========================================"
echo "           验证总结"
echo "========================================"

# 检查是否所有关键修复都成功
CRITICAL_CHECKS=0
TOTAL_CHECKS=0

# 检查关键修复项
if sudo mount -o loop "$FIXED_ISO" "$ISO_MOUNT" 2>/dev/null; then
    sudo unsquashfs -d "$SQUASHFS_DIR" "$ISO_MOUNT/LiveOS/squashfs.img" >/dev/null 2>&1
    sudo mount -o loop "$SQUASHFS_DIR/LiveOS/rootfs.img" "$ROOTFS_MOUNT" 2>/dev/null
    
    # EFI目录修复
    if sudo grep -q 'efi_dir = "centos"' "$ROOTFS_MOUNT/usr/lib64/python2.7/site-packages/pyanaconda/installclasses/fedora.py" 2>/dev/null; then
        ((CRITICAL_CHECKS++))
    fi
    ((TOTAL_CHECKS++))
    
    # 双启动支持
    if [ -f "$ISO_MOUNT/images/efiboot.img" ] && [ -f "$ISO_MOUNT/isolinux/isolinux.bin" ]; then
        ((CRITICAL_CHECKS++))
    fi
    ((TOTAL_CHECKS++))
    
    sudo umount "$ROOTFS_MOUNT" 2>/dev/null || true
    sudo umount "$ISO_MOUNT" 2>/dev/null || true
fi

if [ $CRITICAL_CHECKS -eq $TOTAL_CHECKS ]; then
    log_success "所有关键修复验证通过"
    echo
    echo "🎯 修复效果:"
    echo "• ✅ 不再出现fedora EFI目录错误"
    echo "• ✅ UEFI安装过程正常"
    echo "• ✅ 支持Secure Boot"
    echo "• ✅ 保持Legacy BIOS兼容"
    echo
    echo "📋 使用建议:"
    echo "1. 可以安全使用此ISO进行UEFI安装"
    echo "2. 支持在Secure Boot环境下启动"
    echo "3. 安装时选择CentOS相关选项"
    echo "4. 建议先在虚拟机中测试"
else
    log_warning "部分修复可能未完全生效"
    echo
    echo "⚠️  建议:"
    echo "1. 检查修复脚本是否正确执行"
    echo "2. 重新运行修复脚本"
    echo "3. 联系技术支持"
fi

echo
echo "验证完成时间: $(date)"
echo "========================================"


